package com.toy.controller;



import com.alipay.api.domain.ItemVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.toy.entity.Cart;
import com.toy.entity.Product;
import com.toy.entity.User;
import com.toy.exception.ToyMallException;
import com.toy.result.ResponseEnum;
import com.toy.service.CartService;
import com.toy.service.ProductCategoryService;
import com.toy.service.ProductService;
import com.toy.vo.SysResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-08-31
 */
@Controller
@CrossOrigin
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private CartService cartService;

    /**
     * 商品列表
     * @param type
     * @param productCategoryId
     * @param session
     * @return
     */
    @GetMapping("/list/{type}/{id}")
    public ModelAndView list(@PathVariable("type") Integer type,
                             @PathVariable("id") Integer productCategoryId,
                             HttpSession session){
        if (type == null || productCategoryId ==null){
            log.info("【商品列表】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        modelAndView.addObject("productList",
                productService.findAllByTypeAndProductCategoryId(type, productCategoryId));
        modelAndView.addObject("list",
                this.productCategoryService.buildProductCategoryMenu());
        User user = (User) session.getAttribute("user");
        if (user == null){
            //未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        }else {
            //登录用户
            //查询该用户的购物车记录
            modelAndView.addObject("cartList",
                    this.cartService.findCartVOListByUserId(user.getId()));
        }
        return modelAndView;
    }

    /**
     * 搜索商品
     * @param keyWord
     * @param session
     * @return
     */
    @PostMapping("/search")
    public ModelAndView search(String keyWord, HttpSession session){
        if (keyWord == null){
            log.info("【商品搜索】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        //模糊查询数据
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyWord);
        modelAndView.addObject("productList",
                this.productService.list(queryWrapper));
        //商品分类
        modelAndView.addObject("list",
                this.productCategoryService.buildProductCategoryMenu());
        User user = (User) session.getAttribute("user");
        if (user == null){
            //未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        }else {
            //登录用户
            //查询该用户的购物车记录
            modelAndView.addObject("cartList",
                    this.cartService.findCartVOListByUserId(user.getId()));
        }
        return modelAndView;
    }

    /**
     * 商品详情
     * @param id
     * @param session
     * @return
     */
    @GetMapping("/detail/{id}")
    public ModelAndView detail(@PathVariable("id") Integer id, HttpSession session){
        if (id == null){
            log.info("【商品详情】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productDetail");
        //查询数据
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        modelAndView.addObject("product",
                this.productService.getOne(queryWrapper));
        //商品分类
        modelAndView.addObject("list",
                this.productCategoryService.buildProductCategoryMenu());
        User user = (User) session.getAttribute("user");
        if (user == null){
            //未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        }else {
            //登录用户
            //查询该用户的购物车记录
            modelAndView.addObject("cartList",
                    this.cartService.findCartVOListByUserId(user.getId()));
        }
        return modelAndView;
    }

    @PostMapping("/saveItem")
    @ResponseBody
    public SysResult saveItem(@RequestBody Product product){
//        System.out.println(product);
        productService.insert(product);
        return SysResult.success();
    }

    @PutMapping("/updateProduct")
    @ResponseBody
    public SysResult updateItemCat(@RequestBody Product itemCat){
        productService.updateItemCat(itemCat);
        return SysResult.success();
    }


    @DeleteMapping("/deleteItemById")
    @ResponseBody
    public SysResult deleteItemCat(Product itemCat){
        productService.deleteItemCat(itemCat);
        return SysResult.success();
    }

    @GetMapping("/{id}")
    @ResponseBody
    public SysResult findProductById(@PathVariable Integer id){
        Product product = productService.findProductById(id);
        return SysResult.success(product);
    }
}

